package uk.ac.ebi.bioinvindex.model.impl.processing;

import org.hibernate.annotations.Target;
import org.hibernate.search.annotations.ContainedIn;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.IndexedTarget;
import uk.ac.ebi.bioinvindex.model.impl.StudyImpl;
import uk.ac.ebi.bioinvindex.model.impl.term.AssayParameterValue;
import uk.ac.ebi.bioinvindex.model.impl.term.AssayTechnology;
import uk.ac.ebi.bioinvindex.model.impl.term.EndPoint;
import uk.ac.ebi.bioinvindex.model.impl.term.PropertyValueImpl;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 30, 2007
 */
@Entity
@DiscriminatorValue("Assay")
@SecondaryTable(
		name = "ASSAY",
		pkJoinColumns = @PrimaryKeyJoinColumn(name = "id")
)
@Indexed(index = "bii")
public class AssayImpl extends ProcessingImpl<MaterialNodeImpl, DataNodeImpl> {

	@Target(AssayTechnology.class)
	@IndexedEmbedded(depth = 1, prefix = "technology_")
	@IndexedTarget(AssayTechnology.class)
	private AssayTechnology technology;

	@Target(EndPoint.class)
	@IndexedEmbedded(depth = 1, prefix = "end_point_")
	@IndexedTarget(EndPoint.class)
	private EndPoint endPoint;

	private Platform platform;

	private Collection<PropertyValueImpl> cascadedValues = new ArrayList<PropertyValueImpl>();

	private Collection<AssayParameterValue> assayParameters = new ArrayList<AssayParameterValue>();

	protected AssayImpl() {
		super();
	}

	public AssayImpl(StudyImpl study) {
		super(study);
		study.addAssay(this);
	}

	public AssayImpl(StudyImpl study, boolean original) {
		super(study, original);
		study.addAssay(this);
	}

	@ManyToOne(
			targetEntity = AssayTechnology.class)
	@JoinColumn(table = "ASSAY", name = "technology", nullable = false)
	public AssayTechnology getTechnology() {
		return technology;
	}

	public void setTechnology(AssayTechnology technology) {
		this.technology = technology;
	}

	@ManyToOne(
			targetEntity = EndPoint.class)
	@JoinColumn(table = "ASSAY", name = "endpoint", nullable = false)
	public EndPoint getEndPoint() {
		return endPoint;
	}

	public void setEndPoint(EndPoint endPoint) {
		this.endPoint = endPoint;
	}

	public Platform getPlatform() {
		return platform;
	}

	public void setPlatform(Platform platform) {
		this.platform = platform;
	}

	@ManyToOne(targetEntity = StudyImpl.class)
	@JoinColumn(name = "STUDY_ID", nullable = false, insertable = false, updatable = false)
	@ContainedIn
	public StudyImpl getStudy() {
		return super.getStudy();
	}

	@OneToMany(
			targetEntity = uk.ac.ebi.bioinvindex.model.impl.term.PropertyValueImpl.class
	)
	@JoinTable(
			name = "Assay2PropertyValue",
			joinColumns = {@JoinColumn(name = "ASSAY_ID")},
			inverseJoinColumns = @JoinColumn(name = "PV_ID")
	)
	public Collection<PropertyValueImpl> getCascadedPropertyValues() {
		return this.cascadedValues;
	}

	public void addCascadedPropertyValue(PropertyValueImpl value) {
		this.cascadedValues.add(value);
	}

	protected void setCascadedPropertyValues(Collection<PropertyValueImpl> values) {
		this.cascadedValues = values;
	}

	public boolean removeCascadedPropertyValue(PropertyValueImpl value) {
		return cascadedValues.remove(value);
	}

	@OneToMany(targetEntity = uk.ac.ebi.bioinvindex.model.impl.term.AssayParameterValue.class,
			mappedBy = "assay", cascade = CascadeType.ALL)
	public Collection<AssayParameterValue> getAssayParameterValues() {
		return assayParameters;
	}

	public void addAssayParameterValue(AssayParameterValue value) {
		this.assayParameters.add(value);
	}

	public boolean removeAssayParameterValue(AssayParameterValue value) {
		return assayParameters.remove(value);
	}

	protected void setAssayParameterValues(Collection<AssayParameterValue> assayParameters) {
		this.assayParameters = assayParameters;
	}


}
